package com.amz.analyzer.keyword.service.impl;

import cn.hutool.core.util.StrUtil;
import com.amz.analyzer.keyword.entity.SearchRank;
import com.amz.analyzer.keyword.mapper.SearchRankMapper;
import com.amz.analyzer.keyword.req.SearchRankReq;
import com.amz.analyzer.keyword.service.ISearchRankService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Freddy
 * @since 2020-08-20
 */
@Service
public class SearchRankServiceImpl extends ServiceImpl<SearchRankMapper, SearchRank> implements ISearchRankService {

    @Override
    public IPage<SearchRank> search(SearchRankReq searchRankReq) {

        QueryWrapper<SearchRank> queryWrapper = new QueryWrapper();
        // 数据日期，必填项
        queryWrapper.eq(SearchRank.FieldNames.stat_date.name(), searchRankReq.getStatDate());

        //关键词
        if (StrUtil.isNotBlank(searchRankReq.getKeyword())) {
            queryWrapper.like(SearchRank.FieldNames.keyword.name(), searchRankReq.getKeyword());
        }

        // 排除关键词
        String excludeKeyword = searchRankReq.getExcludeKeyword();
        if (StrUtil.isNotBlank(excludeKeyword)) {
            String[] exKeywords = excludeKeyword.split(StrUtil.COMMA);
            for (String exWord : exKeywords) {
                queryWrapper.notLike(SearchRank.FieldNames.keyword.name(), exWord);
            }
        }

        // 周排名
        if (StrUtil.isNotBlank(searchRankReq.getWeekRankMin())) {
            queryWrapper.gt(SearchRank.FieldNames.week_rank.name(), searchRankReq.getWeekRankMin());
        }
        if (StrUtil.isNotBlank(searchRankReq.getWeekRankMax())) {
            queryWrapper.lt(SearchRank.FieldNames.week_rank.name(), searchRankReq.getWeekRankMax());
        }

        // 上周排名
        if (StrUtil.isNotBlank(searchRankReq.getLastWeekRankMin())) {
            queryWrapper.gt(SearchRank.FieldNames.last_week_rank.name(), searchRankReq.getLastWeekRankMin());
        }
        if (StrUtil.isNotBlank(searchRankReq.getLastWeekRankMax())) {
            queryWrapper.lt(SearchRank.FieldNames.last_week_rank.name(), searchRankReq.getLastWeekRankMax());
        }

        // 涨幅
        if (StrUtil.isNotBlank(searchRankReq.getIncreaseNumberMin())) {
            queryWrapper.gt(SearchRank.FieldNames.increase_number.name(), searchRankReq.getIncreaseNumberMin());
        }
        if (StrUtil.isNotBlank(searchRankReq.getIncreaseNumberMax())) {
            queryWrapper.lt(SearchRank.FieldNames.increase_number.name(), searchRankReq.getIncreaseNumberMax());
        }

        // 点击分享
        if (StrUtil.isNotBlank(searchRankReq.getClickShareMin())) {
            queryWrapper.gt(SearchRank.FieldNames.click_share.name(), searchRankReq.getClickShareMin());
        }
        if (StrUtil.isNotBlank(searchRankReq.getClickShareMax())) {
            queryWrapper.lt(SearchRank.FieldNames.click_share.name(), searchRankReq.getClickShareMax());
        }

        // 转化分享
        if (StrUtil.isNotBlank(searchRankReq.getTransShareMin())) {
            queryWrapper.gt(SearchRank.FieldNames.trans_share.name(), searchRankReq.getTransShareMin());
        }
        if (StrUtil.isNotBlank(searchRankReq.getTransShareMax())) {
            queryWrapper.lt(SearchRank.FieldNames.trans_share.name(), searchRankReq.getTransShareMax());
        }

        if (StrUtil.isNotBlank(searchRankReq.getDate())) {
            queryWrapper.gt(SearchRank.FieldNames.create_time.name(), searchRankReq.getDate());
        } else {
            // 获取最大日期，再进行条件查询
        }


        // 处理排序
        if (StrUtil.isNotBlank(searchRankReq.getSortFieldName())) {
            String filedName = StrUtil.toUnderlineCase(searchRankReq.getSortFieldName());
            if (1 == searchRankReq.getAsc()) {
                queryWrapper.orderByAsc(filedName);
            } else {
                queryWrapper.orderByDesc(filedName);
            }
        } else {
            // 默认按周搜索排行进行排序
            queryWrapper.orderByAsc(SearchRank.FieldNames.week_rank.name());
        }

        log.debug(searchRankReq.toString());
        log.debug(StrUtil.format("当前页：{}，每页条数：{}", searchRankReq.getPageNo(), searchRankReq.getPageSize()));

        IPage<SearchRank> page = new Page<>(searchRankReq.getPageNo(), searchRankReq.getPageSize());

        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<String> getStatDateList() {
        return baseMapper.getStatDateList();
    }

}
